package com.roger.ats.init;

import android.os.RemoteException;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.util.Log;

import com.roger.ats.init.cases.AccountInit;
import com.roger.ats.init.cases.AppInit;
import com.roger.ats.init.cases.HomeScreenInit;
import com.roger.ats.init.cases.SettingsInit;
import com.roger.ats.init.util.LogUtils;
import com.roger.uia.helper.UiBase;
import com.roger.uia.helper.UiWatch;
import com.roger.uia.helper.util.FileUtils;
import com.roger.uia.helper.manager.Package;
import com.roger.uia.helper.util.StrUtils;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import java.io.File;
import java.text.SimpleDateFormat;

import static com.roger.uia.helper.UiBase.mDevice;
import static com.roger.uia.helper.UiBase.waitTime;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Main {
    private static final String TAG = "Test" + Main.class.getSimpleName();
    private static LogUtils logUtils = new LogUtils();

    @BeforeClass
    public static void beforeClass() throws Throwable{
        Package.pac().grantAllPerms(InstrumentationRegistry.getTargetContext().getPackageName());
        initLogs();
        if(!mDevice.isScreenOn()) mDevice.wakeUp();
        for(int i=0;i<5;i++){
            if(UiBase.existObj(UiBase.objSelect[0],"已连接到 USB 调试")) {
                mDevice.swipe(StrUtils.SCREEN_W/2,StrUtils.SCREEN_H-20,StrUtils.SCREEN_W/2,20,50);
                SystemClock.sleep(waitTime/5);
            }else{
                break;
            }
        }
    }

    @Before
    public void setUp() {
        Log.d(TAG, "-- set up --");
        if(mDevice.isNaturalOrientation()) {
            try {
                mDevice.setOrientationLeft();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        UiWatch.getWatcher().runErrorWatcher();
        UiWatch.getWatcher().runSpecialWatcher();
    }

    @After
    public void tearDown() {
        Log.d(TAG, "-- tear down --");
        UiWatch.getWatcher().removeErrorWatcher();
        UiWatch.getWatcher().removeSpecialWatcher();
    }

    @AfterClass
    public static void afterClass() throws Throwable{
        mDevice.setOrientationLeft();
    }

    @Test
    public void main(){
        checkDCIM();
        AppInit.disableApp();
        AppInit.grantApp();
        AppInit.closeAutoUpdate();

        SettingsInit.connWifi();
        AccountInit.launchPOP3();
        AccountInit.launchIMAP();
        AccountInit.launchLENOVO();

        SettingsInit.setMobileData();
        SettingsInit.setBTName();
        SettingsInit.setAutoBrightness();
        SettingsInit.setBrightness();
        SettingsInit.setDesktop();
        SettingsInit.setNavigation();
        SettingsInit.changeWallpaper();
        SettingsInit.setLock();
        SettingsInit.setBattery();
        SettingsInit.setMultiUser();
        SettingsInit.setKeyBoard();
        SettingsInit.setLenovoUpdate();

        HomeScreenInit.removeWidget();
        HomeScreenInit.openLenovoEntertainment();
        HomeScreenInit.checkLeftScreen();
        HomeScreenInit.setRecordVideo();

        AppInit.openAppStore();
        AppInit.openCalendar();
        AppInit.openSoundRecorder();
        AppInit.openNBUFiles();
        AppInit.addCallLogs();
        AppInit.addCalendarEvent();

        SettingsInit.setWellBeing();

    }

    @Test
    public void noLock(){
        mDevice.pressHome();
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3], StrUtils.settingDashBoard, 10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard, UiBase.objSelect[2], StrUtils.settingSecurity);
            UiBase.waitObj(UiBase.objSelect[3], StrUtils.settingList, 3);
            UiBase.scrollReidClickObj(StrUtils.settingList, UiBase.objSelect[2], StrUtils.ScreenLock);
            UiBase.waitClickObj(UiBase.objSelect[2], StrUtils.lockNone, 3);
        }catch (Throwable e){
            e.printStackTrace();
        }finally {
            mDevice.pressHome();
        }
    }

    private static void initLogs() throws Throwable{
        FileUtils.deleteFile(UiBase.filePath+"ATS/logs/AtsLogs.txt"); // del old logs
        logUtils.printLog("[version]= V1.1.0_20200113");
        logUtils.printLog("[model name]=" + UiBase.mDevice.executeShellCommand("getprop ro.product.model").replace("\n",""));
        logUtils.printLog("[serial no]=" + UiBase.mDevice.executeShellCommand("getprop ro.serialno").replace("\n",""));
        logUtils.printLog("[build info]=" + UiBase.mDevice.executeShellCommand("getprop ro.build.description").replace("\n",""));
    }

    private static void checkDCIM(){
        if(Package.pac().isAppInstalled("com.android.chrome")){
            FileUtils.delFolder(new File(UiBase.filePath + "DCIM/Camera"));
        }
    }


    @Test
    public void debug() throws Throwable {
        AppInit.addCallLogs();

    }


}
